<template>
  <div class="loginWrap">
    <div class="head">
      <h1>登录页</h1>
    </div>
    <div class="loginBox">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="登录" name="first">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm()">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="注册" name="second">
          <el-form
            :model="regForm"
            :rules="regRules"
            ref="regForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="regForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="regForm.password"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
              <el-input v-model="regForm.nickname"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="regSubmit">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {getLogin,getReg} from "../api/login"
export default {
  data() {
    return {
      activeName: "first",
      ruleForm: {
        username: "",
        password: "",
      },
      regForm: {
        username: "",
        password: "",
        nickname: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      regRules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
      },
    };
  },
  methods: {
      async getLogin(){
          const res=await getLogin('/login',this.ruleForm)
          const {code,token,nickname,msg}=res.data
          if(code===200){
              this.$notify.success(msg)
              localStorage.setItem('token',token)
              localStorage.setItem('user',nickname)
              this.$router.push('/home')
          }else{
              this.$notify.error(msg)
          }
      },
      async getReg(){
          const res=await getReg('/register',this.regForm)
          console.log(res)
          const {code,msg}=res.data
          if(code===200){
              this.$notify.success(msg)
              this.activeName='first'
          }else{
              this.$notify.error(msg)
          }
      },
    submitForm() {
        this.getLogin()
        console.log(this.ruleForm);
    },
    regSubmit(){
        console.log(this.regForm);
        this.getReg()
    }
  },
};
</script>

<style lang='scss'>
.loginWrap {
  width: 100%;
  height: 100%;
  .loginBox {
    width: 600px;
    height: 400px;
    background: #fff;
    border-radius: 20px;
    border: 1px solid darkblue;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px;
  }
}
</style>